package cn.yunyichina.provider.message.alipay.service.impl;

import cn.yunyichina.provider.iface.entity.alipay.AlipayRequest;
import cn.yunyichina.provider.iface.entity.alipay.AlipayResponse;
import cn.yunyichina.provider.iface.entity.common.ResCommon;
import cn.yunyichina.provider.message.alipay.service.AlipayCardMessageService;
import cn.yunyichina.provider.pay.alipay.config.Alipay;
import cn.yunyichina.provider.pay.alipay.constant.ResponseEnum;
import cn.yunyichina.provider.pay.alipay.service.BaseService;
import cn.yunyichina.utils.dozer.Dozer;
import com.alibaba.fastjson.JSON;
import com.alipay.api.AlipayApiException;
import com.alipay.api.AlipayClient;
import com.alipay.api.DefaultAlipayClient;
import com.alipay.api.request.AlipayEcoMedicalcareCommonDataSyncRequest;
import com.alipay.api.response.AlipayEcoMedicalcareCommonDataSyncResponse;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

/**
 * Created by Lullaby on 2017/7/25
 */
@Service
public class AlipayCardMessageServiceImpl extends BaseService implements AlipayCardMessageService {

    private static Logger logger = LoggerFactory.getLogger(AlipayCardMessageServiceImpl.class);

    public ResCommon<AlipayResponse> sendCardMessage(AlipayRequest request) {
        logger.info("AlipayCardMessageServiceImpl.sendRegCardMessage.request -> {}", JSON.toJSONString(request));

        AlipayClient alipayClient = new DefaultAlipayClient(
                getConfig(Alipay.ALIPAY_GATEWAY_OPENAPI),
                request.getAppId(),
                request.getPrivateKey(),
                request.getFormat(),
                request.getCharset(),
                request.getPublicKey()
        );

        ResCommon<AlipayResponse> response = new ResCommon<>();

        AlipayEcoMedicalcareCommonDataSyncRequest alipayRequest = new AlipayEcoMedicalcareCommonDataSyncRequest();
        alipayRequest.setBizContent(request.getBizContent());
        logger.info("AlipayCardMessageServiceImpl.sendRegCardMessage.alipayRequest -> {}", JSON.toJSONString(alipayRequest));

        try {
            AlipayEcoMedicalcareCommonDataSyncResponse alipayResponse = alipayClient.execute(alipayRequest);
            logger.info("AlipayCardMessageServiceImpl.sendRegCardMessage.alipayResponse -> {}", JSON.toJSONString(alipayResponse));

            AlipayResponse result = Dozer.convert(alipayResponse, AlipayResponse.class);
            logger.info("AlipayCardMessageServiceImpl.sendRegCardMessage.result -> {}", JSON.toJSONString(result));

            if (alipayResponse.isSuccess() && "10000".equals(alipayResponse.getCode())) {
                response.setResult(result);
            } else {
                response.setResultCodeAndMessageAndResult(
                        ResponseEnum.ALIPAY_CARD_MESSAGE_FAIL.getCode(),
                        ResponseEnum.ALIPAY_CARD_MESSAGE_FAIL.getMessage(),
                        result
                );
            }
        } catch (AlipayApiException e) {
            logger.error("AlipayCardMessageServiceImpl.sendRegCardMessage.AlipayApiException", e);
            response.setResultCodeAndMessage(
                    ResponseEnum.ALIPAY_CARD_MESSAGE_EXCEPTION.getCode(),
                    ResponseEnum.ALIPAY_CARD_MESSAGE_EXCEPTION.getMessage()
            );
        }

        return response;
    }

}
